package com.bjdd.jdy.ui.map;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.RouteLine;
import com.baidu.mapapi.search.route.BikingRouteResult;
import com.baidu.mapapi.search.route.DrivingRouteLine;
import com.baidu.mapapi.search.route.DrivingRoutePlanOption;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.IndoorRouteResult;
import com.baidu.mapapi.search.route.MassTransitRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRouteResult;
import com.bjdd.jdy.Constant;
import com.bjdd.jdy.R;
import com.bjdd.jdy.baidu.overlayutil.DrivingRouteOverlay;
import com.bjdd.jdy.base.BaseActivity;
import com.bjdd.jdy.entity.RoutePoint;
import com.bjdd.jdy.util.L;
import com.bjdd.jdy.util.StatusBarUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MapActivity extends BaseActivity<MapPresenter, MapModel> implements MapContract.View {


    @Bind(R.id.toolbar_back)
    ImageView toolbarBack;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.mv_map)
    MapView mvMap;
    private BaiduMap mBaiduMap;
    private RoutePoint point;

    @Override
    public int getLayoutId() {
        return R.layout.activity_map;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this,getResources().getColor(R.color.toolbar_bg),0);
        title.setText("地图");
        mBaiduMap = mvMap.getMap();
        //普通地图
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        Intent intent = this.getIntent();
        point = (RoutePoint) intent.getExtras().get(Constant.POINTS);
        RoutePlan();
    }

    private void RoutePlan(){
        RoutePlanSearch mSearch = RoutePlanSearch.newInstance();
        OnGetRoutePlanResultListener listener = new OnGetRoutePlanResultListener() {
            @Override
            public void onGetWalkingRouteResult(WalkingRouteResult walkingRouteResult) {

            }

            @Override
            public void onGetTransitRouteResult(TransitRouteResult transitRouteResult) {

            }

            @Override
            public void onGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult) {

            }

            @Override
            public void onGetDrivingRouteResult(DrivingRouteResult drivingRouteResult) {
                L.d("驾车路线规划");
                List<DrivingRouteLine> routeLines = drivingRouteResult.getRouteLines();
                L.d("routeLines长度:"+routeLines.size());
                DrivingRouteOverlay overlay = new MyDrivingRouteOverlay(mBaiduMap);
                mBaiduMap.setOnMarkerClickListener(overlay);
                overlay.setData(routeLines.get(0));
                overlay.addToMap();
                overlay.zoomToSpan();
                mSearch.destroy();

            }

            @Override
            public void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult) {

            }

            @Override
            public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) {

            }
        };
        mSearch.setOnGetRoutePlanResultListener(listener);
        LatLng slatLng = new LatLng(point.getStartLat(),point.getStartLng());
        PlanNode stNode = PlanNode.withLocation(slatLng);
        LatLng elatLng = new LatLng(point.getEndLat(),point.getEndLng());
        PlanNode enNode = PlanNode.withLocation(elatLng);
        mSearch.drivingSearch(new DrivingRoutePlanOption()
        .from(stNode)
        .to(enNode));
    }

    @Override
    public void LocationBack(BDLocation location) {
        L.d("经纬度：" + location.getLatitude() + "----" + location.getLongitude());
        LatLng cenpt = new LatLng(location.getLatitude(), location.getLongitude());
        //定义地图状态
        MapStatus mapStatus = new MapStatus.Builder()
                .target(cenpt)
                .zoom(15)
                .build();
        //定义MapStatusUpdate对象，以便描述地图状态将要发生的变化
        MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mapStatus);
        //改变地图状态
        mBaiduMap.setMapStatus(mapStatusUpdate);
        BitmapDescriptor centMark = BitmapDescriptorFactory
                .fromResource(R.mipmap.location);
        //构建MarkerOption,用于在地图上添加marker
        OverlayOptions options = new MarkerOptions()
                .position(cenpt)
                .icon(centMark);
        mBaiduMap.addOverlay(options);
    }

    @Override
    protected void onDestroy() {
        mvMap.onDestroy();
        super.onDestroy();

    }

    @Override
    protected void onResume() {
        mvMap.onResume();
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mvMap.onPause();
    }


    @OnClick(R.id.toolbar_back)
    public void onViewClicked() {
        finish();
    }

    private class MyDrivingRouteOverlay extends DrivingRouteOverlay {

        public MyDrivingRouteOverlay(BaiduMap baiduMap) {
            super(baiduMap);
        }

        @Override
        public BitmapDescriptor getStartMarker() {
            /*if (useDefaultIcon) {
                return BitmapDescriptorFactory.fromResource(R.mipmap.location);
            }
            return null;*/
            return BitmapDescriptorFactory.fromResource(R.mipmap.location);
        }

        @Override
        public BitmapDescriptor getTerminalMarker() {
           /* if (useDefaultIcon) {

            }*/
            return BitmapDescriptorFactory.fromResource(R.mipmap.location);
            //return null;
        }
    }
}
